package com.ruoyi.framework.pay;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class TestDemo {


    public static void main(String[] args) {
        startPay();
        //getTransaction();
    }

    public static void getTransaction(){
        try{

            String token = "JOdWjSaaOtfoHbi8ojtZoKPAXoPObBu8";//Obtained from Initiate Transaction request
            String query = "https://poliapi.apac.paywithpoli.com/api/v2/Transaction/GetTransaction?token=" +token;

            URL url = new URL(query);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);

            String accesscode = "SS64012090:nC5!PfcYX3Rzd"; //ADD YOUR OWN CREDENTIALS
            String encodedAuthString = Base64.getEncoder().encodeToString(accesscode.getBytes());

            conn.setRequestProperty("authorization", "Basic " + encodedAuthString);

            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("GET");

            // read the response
            InputStream in = new BufferedInputStream(conn.getInputStream());

            byte[] contents = new byte[1024];
            int bytesRead = 0;

            String poliResponse = "";

            while((bytesRead = in.read(contents)) != -1)
            {
                poliResponse += new String(contents, 0, bytesRead);
            }


            System.out.print(poliResponse);

            in.close();
            conn.disconnect();

        }
        catch(IOException ex)
        {
            System.out.print(ex);
        }

    }

    public static void startPay() {
        try {

            String query = "https://poliapi.apac.paywithpoli.com/api/v2/Transaction/Initiate";


            String json = "{\n" +
                    "  \"Amount\": \"1.2\",\n" +
                    "  \"CurrencyCode\": \"NZD\",\n" +
                    "  \"MerchantReference\": \"CustomerRef12345\",\n" +
                    "  \"MerchantHomepageURL\": \"https://www.mycompany.com\",\n" +
                    "  \"SuccessURL\": \"https://www.mycompany.com/Success\",\n" +
                    "  \"FailureURL\": \"https://www.mycompany.com/Failure\",\n" +
                    "  \"CancellationURL\": \"https://www.mycompany.com/Cancelled\",\n" +
                    "  \"NotificationURL\": \"https://www.mycompany.com/nudge\"\n" +
                    "}";

            URL url = new URL(query);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);

            String accesscode = "SS64012090:nC5!PfcYX3Rzd"; //ADD YOUR CREDENTIALS
            String encodedAuthString = Base64.getEncoder().encodeToString(accesscode.getBytes());

            conn.setRequestProperty("authorization", "Basic " + encodedAuthString);
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");

            OutputStream os = conn.getOutputStream();
            os.write(json.getBytes("UTF-8"));
            os.close();

            // read the response
            InputStream in = new BufferedInputStream(conn.getInputStream());

            byte[] contents = new byte[1024];
            int bytesRead;

            String poliResponse = "";

            while ((bytesRead = in.read(contents)) != -1) {
                poliResponse += new String(contents, 0, bytesRead);
            }

            System.out.print(poliResponse);//Process the JSON Response and Redirect to NavigateURL

            in.close();
            conn.disconnect();

        } catch (IOException ex) {
            System.out.print(ex);
        }


    }
}
